import re
import requests
import time
import csv

# url='https://movie.douban.com/top250?start=25&filter='
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
fq=open('./doubanmovics.csv','w+',newline='',encoding='utf-8')
writer = csv.writer(fq)
writer.writerow(['排名','电影名称','演员','评分','评价','引用'])
#电影信息爬取
for i in range(0,226,25):
    url='https://movie.douban.com/top250?start={}&filter='.format(str(i))
    rqq=requests.get(url,headers=headers)
    print(rqq)
    ranks=re.findall('<em class="">(.*?)</em>',rqq.text,re.S)#re.S将\n当作普通字符处理,可以将多行字符串看成整体
    names=re.findall('<a href=".*?" class="">.*?<span class="title">(.*?)</span>',rqq.text,re.S)  #re.complie将正则表达式编译为一个pattern对象
    actors=re.findall('&nbsp;&nbsp;(.*?)<br>',rqq.text)  #主演
    acores=re.findall('<span class="rating_num" property="v:average">(.*?)</span>',rqq.text)  #评分
    evalutes=re.findall('<span>(.*?)评价</span>',rqq.text)  #评价人数
    quites=re.findall('<span class="inq">(.*?)</span>',rqq.text)  #引用
    for rank,name,actor,acore,evalute,quite in zip(ranks,names,actors,acores,evalutes,quites):
        # data = {
        #     '排名':rank,
        #     '电影名称':name,
        #     '演员':actor,
        #     '评分':acore,
        #     '评价':evalute,
        #     '引用':quite
        # }
        # print(data)
        writer.writerow([rank,name,actor.replace('&nbsp;',''),acore,evalute,quite])
    time.sleep(1)
fq.close()